package com.financing.wap.service.impl;

import com.financing.wap.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.common.BizEnums;
import com.financing.wap.dao.ISystemDao;
import com.financing.wap.service.IBankCardService;
import com.financing.wap.service.ICommonService;
import com.financing.wap.service.IFpBiddingReceiptService;
import com.financing.wap.service.IOrderService;
import com.financing.wap.service.IPaymentService;
import com.financing.wap.service.IUserService;
import com.financing.wap.service.IYouDanAmountDetailService;



@Service("commonServiceImpl")
public class CommonServiceImpl implements ICommonService{

	@Autowired
	private IBankCardService bankCardServiceImpl;// 用户银行卡服务
	
	@Autowired
	private IUserService userService;//用户服务
	
	@Autowired
	private IPaymentService paymentServiceImpl;//支付流水服务
	
	
	@Autowired
	IFpBiddingReceiptService fpBiddingReceiptService;//竞价服务
	
	@Autowired
	IYouDanAmountDetailService youDanAmountDetailServiceImpl;//有单金额明细表 服务
	
	@Autowired
	private IOrderService orderService; //订单服务
	
	@Autowired
	private ISystemDao systemDao;
	
	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public String confirmbindbankcard(BankCardDTO bankCardDTO, UserDTO user) throws Exception {
		//添加用户绑卡记录
		int id =bankCardServiceImpl.insertBankCard(bankCardDTO);
		bankCardDTO.setId(id);
		
		//修改用户绑卡信息
		userService.updateUser(user);
		
		return "SUCCESS";
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void orderBZJDealSuccessNew(FpBiddingReceiptDTO dto,
			FpPaymentDTO paymentUp,FpYouDanAmountDetailDTO youDanAmountDetailDTO) throws Exception{
		paymentUp.setPayStatus(BizEnums.PayEnums.PAY_STATUS_SUCC);//支付状态
		paymentServiceImpl.updatePayment(paymentUp);//更新支付流水状态 
		
		dto.setFlagBail(BizEnums.BiddingReceiptEnums.FLAG_BAIL_YES);
		fpBiddingReceiptService.updateBiddingReceipt(dto);//更新 竞价状态
		
		youDanAmountDetailServiceImpl.insertYouDanAmountDetail(youDanAmountDetailDTO);
		
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void orderBZJDealFailNew(FpBiddingReceiptDTO dto,
			FpPaymentDTO paymentUp) throws Exception {
		paymentUp.setPayStatus(BizEnums.PayEnums.PAY_STATUS_FAIL);//支付状态
		paymentServiceImpl.updatePayment(paymentUp);//更新支付流水状态 
		
		dto.setFlagBail(BizEnums.BiddingReceiptEnums.FLAG_BAIL_NO);//是否交保证金 1是 2否
		fpBiddingReceiptService.updateBiddingReceipt(dto);
		
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void orderYJDealSuccessNew(OrderDTO dto,
			FpPaymentDTO paymentUp,
			FpYouDanAmountDetailDTO youDanAmountDetailDTO) throws Exception {
		paymentUp.setPayStatus(BizEnums.PayEnums.PAY_STATUS_SUCC);//支付状态
		paymentServiceImpl.updatePayment(paymentUp);//更新支付流水状态 
		
		//更新下订单表的O_YOU_DAN_RECEIVED_REBATE字段 更新为支付金额
		orderService.updateOrder(dto);
		
		//youDanAmountDetailServiceImpl.insertYouDanAmountDetail(youDanAmountDetailDTO);
		
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void orderYJDealFailNew(OrderDTO dto,
			FpPaymentDTO paymentUp) throws Exception {
		paymentUp.setPayStatus(BizEnums.PayEnums.PAY_STATUS_FAIL);//支付状态
		paymentServiceImpl.updatePayment(paymentUp);//更新支付流水状态 
		
		//TODO 修改订单状态
		
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public String withdrawalsDealSuccessNew(FpPaymentDTO payment,
			FpYouDanAmountDetailDTO youDanAmountDetailDTO) throws Exception{
		
		//更新 有单金额明细表
		youDanAmountDetailServiceImpl.updateYouDanAmountDetail(youDanAmountDetailDTO);
		
		//添加线下交易流水
		paymentServiceImpl.insertPayment(payment);
		
		return "SUCCESS";
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void releaseBindbankcard(BankCardDTO bankCardDTO, UserDTO userInfo)
			throws Exception {
		
		//1,系统修改原绑定银行卡记录(userbindedcard)‘默认标志’为“2-已解绑”；
		bankCardServiceImpl.updateBankCard(bankCardDTO);
		
		//2)根据“用户编号、注册手机号”清理用户注册表（user）绑定银行卡痕迹
		userService.updateUserForce(userInfo);
		
	}

	@Override
	public FpVersionDTO getQueryVersionno(String platform) {
		return systemDao.getQueryVersionno(platform);
	}

}
